package com.loto.insurance.oem.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.loto.insurance.oem.web.authority.OEMAuthority;
import com.loto.insurance.oem.web.entity.ProductInfo;
import com.loto.insurance.oem.web.entity.Pager;
import com.loto.insurance.oem.web.entity.SessionUser;
import com.loto.insurance.oem.web.enumeration.InsTypeEnum;
import com.loto.insurance.oem.web.service.ProductInfoService;

@Controller
@SessionAttributes("oem_user")
public class ProductController {
  @Autowired
  ProductInfoService productInfoService;

  @OEMAuthority
  @RequestMapping(value = "/product/air", method = RequestMethod.GET)
  public String productListAir(
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  Model model, @ModelAttribute("oem_user") SessionUser sessionUser) {
	model.addAllAttributes(prepair(sessionUser.getUserId(), InsTypeEnum.AIR,
	    page));
	return "product/product_list";
  }

  @OEMAuthority
  @RequestMapping(value = "/product/travel", method = RequestMethod.GET)
  public String productListTravel(
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  Model model, @ModelAttribute("oem_user") SessionUser sessionUser) {

	model.addAllAttributes(prepair(sessionUser.getUserId(), InsTypeEnum.TRAVEL,
	    page));

	return "product/product_list";
  }

  private Map<String, Object> prepair(long custId, InsTypeEnum insType, int page) {
	Map<String, Object> map = new HashMap<String, Object>();
	Map<String, Object> params = new HashMap<String, Object>();
	Pager pager = new Pager();

	params.put("custId", custId);
	params.put("productTypeFid", insType.getIndex());

	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	ArrayList<ProductInfo> productList = productInfoService
	    .findAllOrdersByCust(params);

	pager.setRowCount(productInfoService.findAllOrdersByCustCount(params));

	map.put("productType", insType.toString());
	map.put("pager", pager);
	map.put("productList", productList);
	map.put("pageId", insType.getIndex());

	return map;
  }

  @OEMAuthority
  @RequestMapping(value = "/product/detail", method = RequestMethod.GET)
  public String productDetail(
	  @RequestParam(value = "id", required = true) long productId, Model model,
	  @ModelAttribute("oem_user") SessionUser sessionUser) {
	Map<String, Object> params = new HashMap<String, Object>();
	params.put("custId", sessionUser.getUserId());
	params.put("productId", productId);
	ProductInfo productInfo = productInfoService.findProductById(params);
	if (productInfo != null) {
	  model.addAttribute("product", productInfo);
	  model.addAttribute("productName", productInfo.getProductName());
	}
	return "product/product_detail";
  }

  @OEMAuthority
  @RequestMapping(value = "/product/detailinput")
  public String productDetailInput(
	  @RequestParam(value = "insType", required = true) int insType,
	  @RequestParam(value = "productId", required = true) long productId,
	  Model model) {
	model.addAttribute("productId", productId);
	model.addAttribute("insType", insType);
	if (insType == InsTypeEnum.AIR.getIndex())
	  return "ctrl/order_input_air";
	else if (insType == InsTypeEnum.TRAVEL.getIndex())
	  return "ctrl/order_input_travel";
	return "ctrl/order_input_air";
  } 
  
}
